package org.zasadnyy.redlions.players.client.ui;

import java.util.Date;
import java.util.List;

import org.zasadnyy.redlions.players.shared.model.TournamentItem;
import org.zasadnyy.redlions.players.shared.remote.PlayersCrudRemote;
import org.zasadnyy.redlions.players.shared.remote.PlayersCrudRemoteAsync;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardPagingPolicy.KeyboardPagingPolicy;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.smartgwt.client.util.SC;

public class EditTournamentsComposite extends Composite {

	interface EditTournamentsCompositeUiBinder extends
			UiBinder<HorizontalPanel, EditTournamentsComposite> {
	}

	private static EditTournamentsCompositeUiBinder uiBinder = GWT
			.create(EditTournamentsCompositeUiBinder.class);

	private PlayersCrudRemoteAsync servise = GWT
			.create(PlayersCrudRemote.class);

	@UiField(provided = true)
	TournamentEditPanel editPanel;

	@UiField(provided = true)
	CellList<TournamentItem> cellList;

	public static class TournamentCell extends AbstractCell<TournamentItem> {
		@Override
		public void render(Context context, TournamentItem value,
				SafeHtmlBuilder sb) {
			if (value == null) {
				return;
			}

			sb.appendHtmlConstant("<table>");

			// Add the contact image.
			sb.appendHtmlConstant("<tr><td rowspan='3'>");
			// sb.appendHtmlConstant(imageHtml);
			sb.appendHtmlConstant("</td>");

			// Add the name and address.
			sb.appendHtmlConstant("<td style='font-size:95%;'>");
			sb.appendEscaped(value.getTitle());
			sb.appendHtmlConstant("</td></tr><tr><td>");

			Date playsFrom = value.getTime();
			if (playsFrom != null) {
				sb.appendEscaped(DateTimeFormat.getFormat(
						PredefinedFormat.DATE_LONG).format(playsFrom));
			}
			sb.appendHtmlConstant("</td></tr></table>");
		}
	}

	public EditTournamentsComposite() {

		editPanel = new TournamentEditPanel();
		cellList = new CellList<TournamentItem>(new TournamentCell());

		initWidget(uiBinder.createAndBindUi(this));

		loadTournaments();

		cellList.setKeyboardPagingPolicy(KeyboardPagingPolicy.INCREASE_RANGE);
		cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION);

		// Add a selection model so we can select cells.
		final SingleSelectionModel<TournamentItem> selectionModel = new SingleSelectionModel<TournamentItem>();
		cellList.setSelectionModel(selectionModel);
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						editPanel.setTournament(selectionModel
								.getSelectedObject());
					}
				});

		editPanel
				.addValueChangeHandler(new ValueChangeHandler<TournamentItem>() {

					@Override
					public void onValueChange(
							ValueChangeEvent<TournamentItem> event) {
						loadTournaments();
					}
				});
	}

	private void loadTournaments() {
		servise.getAllTournaments(new AsyncCallback<List<TournamentItem>>() {

			@Override
			public void onSuccess(List<TournamentItem> result) {
				cellList.setRowData(result);
				cellList.redraw();
			}

			@Override
			public void onFailure(Throwable caught) {
				SC.say("Things are bad!");
			}
		});
	}

}
